* Settings
version 16
do "$SSDIMed/scripts/_auxiliary/_project_settings.do"


* ---------------------------------------------------------------------------------------
* Prepare data for figures
* ---------------------------------------------------------------------------------------

* Load data based on one observation per bene
use "$SSDIMed/data/analysis/county-month-age_entry_sample-main.dta", clear
qui sum age_year_covstart_fill
assert r(min)==20 & r(max)==62
sum count
assert r(sum) == 16240776

* limit to Medicare entry at ages 22-62
local sample 2262
local sample_2262 inrange(age_year_covstart_fill, 22, 62)
keep if `sample_`sample''
sum count
assert r(sum) == 15790262

* Collapse to month of Medicare entry
gcollapse (mean) cty_unemp_mean=unemp_rate_county_atapp nat_unemp_mean=unemp_rate_national_atapp (p10) cty_unemp_p10=unemp_rate_county_atapp (p50) cty_unemp_p50=unemp_rate_county_atapp (p90) cty_unemp_p90=unemp_rate_county_atapp (rawsum) count [aw = count], by(covstart_year covstart_month) fast
assert _N == 300
sum count
assert r(sum) == 15790262


* ---------------------------------------------------------------------------------------
* Graph settings
* ---------------------------------------------------------------------------------------

* Set graph style to project default settings
*	Pass as arguments: height width (default 3.5in 6.5in)
do "$SSDIMed/scripts/_auxiliary/_project_grstyle.do" 3.1in 6.5in

* Font size
*grstyle set size  8pt: text_option

* p1: County p90, p50, p10 unemployment rate
* brown, orange, and tan
grstyle set color "140 045 004": p4
grstyle set color "217 072 001": p1
grstyle set color "210 180 140": p5
foreach p in 1 4 5 {
grstyle set symbol i: p`p'
grstyle set symbolsize 2.2pt: p`p'
grstyle set lpattern solid: p`p'
grstyle set linewidth 1.0pt: p`p'
}

* p2: National mean unemployment rate
local p 2
grstyle set color "126 153 180": p`p'
grstyle set symbol i: p`p'
grstyle set symbolsize 2.2pt: p`p'
grstyle set lpattern dash: p`p'
grstyle set linewidth 1.3pt: p`p'

* p3: Arrows and lines
local p 3
grstyle set color gs8: p`p'
grstyle set lpattern solid: p`p'
grstyle set linewidth vthin: p`p'


* -------------------------------------------------------------------------------------------------
* Figure 1: Unemployment rate at application, by month of Medicare entry
* -------------------------------------------------------------------------------------------------

assert inrange(covstart_year, 1993, 2017)

* xvar is month DI benefits start, calculated to be two years before Medicare coverage starts
cap drop xvar
gen double xvar = covstart_year + (month(dofm(covstart_month)) - 1)/12 - 2
assert inrange(floor(xvar), 1993 - 2, 2017 - 2)
qui glevelsof xvar
assert r(J) == 12*(2017 - 1993 + 1)


if 1 {

* Make min and max labels align on both y axes
* Stata tip 93: Handling multiple y axes on twoway graphs: https://journals.sagepub.com/doi/pdf/10.1177/1536867X1101000411

* How many major ticks?
* How much should range extend beyond ticks (as fraction of tick step size)
local y_ticks 11
local y_margin_lo 0.6
local y_margin_hi 0.8

* y1 tick lo, step size, format
local y1_tick_lo 3
local y1_step 1
local y1_fmt "%9.2g"
local y1_unit "%"

* Construct y labels and scales from parameters above
forvalues y = 1/1 {
  local y`y'_tick_hi  = `y`y'_tick_lo' + `y`y'_step' * (`y_ticks' - 1)
  local y`y'_range_lo = `y`y'_tick_lo' - `y`y'_step' * `y_margin_lo'
  local y`y'_range_hi = `y`y'_tick_hi' + `y`y'_step' * `y_margin_hi'
  di "y`y'_tick_hi:  `y`y'_tick_hi'"
  di "y`y'_range_lo: `y`y'_range_lo'"
  di "y`y'_range_hi: `y`y'_range_hi'"
  
  local y`y'_label ylabel(`y`y'_tick_lo'(`y`y'_step')`y`y'_tick_hi', axis(`y') format("`y`y'_fmt'") noticks nolabels labgap(1pt)) 
  local y`y'_scale yscale(`yalt' range(`y`y'_range_lo' `y`y'_range_hi') axis(`y') noline) 
  di `"y`y'_label:  `y`y'_label'"'
  di `"y`y'_scale:  `y`y'_scale'"'
}

* x-axis settings
local xlo 1989
local xhi 2017
local xscale xscale(range(`xlo' `xhi'))
local x_axis xlabel(1991(2)2015) `xscale' xtitle(Month of entitlement, margin(t=+.5))

* style and color for y-axis title/labels
local y1_title_style sf
local y1_title_color black
local y1_label_style it
local y1_label_color gs8

* y-axis labels placed above grid rule (build manually)
local y 1
local y`y'_la
foreach tick of numlist  `y`y'_tick_lo'(`y`y'_step')`y`y'_tick_hi' {
  if `tick' == `y`y'_tick_hi' {
    * white textbox to go behind label
    local y`y'_la `y`y'_la' text(`=`tick' + `y1_step'/100' `xlo' " ", yaxis(`y') place(ne) just(left) box lwidth(none) color(`y1_label_color') width(5) height(3) bcolor(white%70))
    *label
    local y`y'_la `y`y'_la' text(`tick' `xlo' "{`y`y'_label_style':`=string(`tick', "`y`y'_fmt'")'`y1_unit'}", yaxis(`y') place(ne) just(left) box lwidth(none) color(`y1_label_color') bcolor(none))
  }
  else {
    local y`y'_la `y`y'_la' text(`tick' `xlo' "{`y`y'_label_style':`=string(`tick', "`y`y'_fmt'")'}", yaxis(`y') place(ne) just(left) box lwidth(none) color(`y1_label_color') bcolor(none))
  }
}

* y-axis titles, horizontal orientation (build manually)
local y1title text(`=`y1_tick_lo' + `y1_step'*(`y_ticks' - 1 + `y_margin_hi')' `xlo' "{`y1_title_style':Unemployment rate}"
local y1title `y1title', yaxis(1) place(ne) just(left) box lwidth(none) color(`y1_title_color') bcolor(white) margin(b=+.5))
local y1title `y1title' ytitle(" ", axis(1) margin(l=-8))

* y-axis settings
local y_axis1 `y1_label' `y1_la' `y1_scale' `y1title'

* Plot lines
local p50_cty (connected cty_unemp_p50 xvar, pstyle(p1))
local p90_cty (connected cty_unemp_p90 xvar, pstyle(p4))
local p10_cty (connected cty_unemp_p10 xvar, pstyle(p5))
local line_nat (connected nat_unemp_mean xvar, pstyle(p2))

* Label line 1
local text_1 text(5.7 1999.06 "National",     place(n) just(center) box lwidth(none) bcolor(white%90))
local text_1 text(5.7 1999.06 "unemployment", place(s) just(center) box lwidth(none) bcolor(white%90)) `text_1'
local xref 1998 + 4/12
qui sum nat_unemp_mean if xvar == `xref'
local pci_1 (pci `=r(mean)' `=`xref'' `=r(mean) + .55' `=`xref' + .25', pstyle(p3))

* Label line 2
local text_2 text(9.7 2012.1 "Median", place(c) just(center) box lwidth(none) bcolor(white%90))
local xref 2011 + 6/12
qui sum cty_unemp_p50 if xvar == `xref'
local pci_2 (pci `=r(mean)' `=`xref'' `=r(mean) + .3' `=`xref' + .3', pstyle(p3))

* Label line 3
local text_3 text(11.5 2012.9 "90{superscript:th} percentile", place(e) just(center) box lwidth(none) width(1.5cm) bcolor(white%90))
local xref 2012 + 4/12
qui sum cty_unemp_p90 if xvar == `xref'
local pci_3 (pci `=r(mean)' `=`xref'' `=r(mean) + .2' `=`xref' + .2', pstyle(p3))

* Label line 4
local text_4 text(5.5 2011 "10{superscript:th} percentile", place(c) just(center) box lwidth(none) width(0.5cm) bcolor(white%90))
local xref 2012 + 0/12
qui sum cty_unemp_p10 if xvar == `xref'
local pci_4 (pci `=r(mean)' `=`xref'' `=r(mean) + -.5' `=`xref' + -.5', pstyle(p3))

local legend legend(off)
local region graphregion(margin(t=5 b=-1))

twoway `p50_cty' `p90_cty' `p10_cty' `line_nat' `pci_1' `pci_2' `pci_3' `pci_4', `x_axis' `y_axis1' `legend' `region' `text_1' `text_2' `text_3' `text_4'

* Export graph
cap mkdir "$SSDIMed/results/figures"
graph export "$SSDIMed/results/figures/entry_month_unemp_`sample'.pdf", as(pdf) fontface("Source Sans Pro") fontdir("$SSDIMed/data/raw/fonts") replace

}

** EOF